﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace Solidi
{
	public class DependencyBuilder : IDependencyBuilder
	{
		private List<Dependency> _decorators = new List<Dependency>();
		private IDependencyRegistry _registry;

		public DependencyBuilder(IDependencyContainer container)
		{
			_registry = container.Registry;
		}

		public DependencyBuilder(IDependencyRegistry registry)
		{
			_registry = registry;
		}

		public DependencyBuilder()
			: this(DefaultRegistry())
		{
			
		}

		public IDependencyRegistry Registry
		{
			get
			{
				return _registry;
			}
		}

		private static IDependencyRegistry DefaultRegistry()
		{
			IDependencyRegistry registry = new DependencyRegistry();

			return registry;
		}

		public void Decorate(Func<IDependencyRegistration, IDependencyRegistration> decorator)
		{
			_decorators.ForEach(dependency => dependency.Decorate(decorator));
		}

		public IDependencyDecorator Register(IDependencyRegistration registration)
		{
			Dependency dependency = new Dependency(_registry, registration);

			_registry.Add(registration.Key, dependency);

			_decorators.Add(dependency);

			return dependency;
		}
	}
}
